﻿using StackExchange.Redis;
using XYS.Core.Models;

namespace XYS.Service.Core.Service
{
    /// <summary>
    /// 缓存服务
    /// </summary>
    public interface ICache
    {
        /// <summary>
        /// redis 数据库 对象
        /// </summary>
        IDatabase RedisDB { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="script"></param>
        /// <param name="keys"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        RedisResult Eval(string script, string[] keys = null, string[] values = null);
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="script"></param>
        /// <param name="keys"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        Task<RedisResult> EvalAsync<T>(string script, string[] keys = null, string[] values = null);

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        bool Exists(string cacheKey);

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task<bool> ExistsAsync(string cacheKey);

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        T Get<T>(string cacheKey);
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="expiration"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        T Get<T>(string cacheKey, TimeSpan? expiration, Func<T> factory);
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task<T> GetAsync<T>(string cacheKey);
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="expiration"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        Task<T> GetAsync<T>(string cacheKey, TimeSpan? expiration, Func<T> factory);
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="expiration"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        Task<T> GetAsync<T>(string cacheKey, TimeSpan? expiration, Func<Task<T>> factory);
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        bool Remove(string cacheKey);
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        Task<bool> RemoveAsync(string cacheKey);
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="cacheValue"></param>
        /// <param name="expiration"></param>
        /// <param name="when"></param>
        /// <returns></returns>
        bool Set<T>(string cacheKey, T cacheValue, TimeSpan? expiration, When when = When.Always);
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="cacheValue"></param>
        /// <param name="expiration"></param>
        /// <param name="when"></param>
        /// <returns></returns>
        Task<bool> SetAsync<T>(string cacheKey, T cacheValue, TimeSpan? expiration, When when = When.Always);
    }
}